package array;

/**
 * Created by fengliejv on 2017/10/27.
 */
public class BestTimetoBuyandSellStockII122 {
    //贪心算法
    public int maxProfit2(int[] prices) {
        int profit = 0;
        for (int i = 1; i < prices.length; i++) {
            int num = prices[i] - prices[i - 1];
            if (num > 0) {
                profit += num;
            }
        }
        return profit;
    }

    //todo 动态规划
    public int maxProfit(int[] prices) {
        int[][] dp = new int[prices.length][2];
        dp[0][0] = -prices[0];
        dp[0][1] = 0;
        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = dp[i][1]-prices[i];
            dp[i][1] = dp[i][0]+prices[i];
        }
        return Math.max(dp[prices.length-1][0],dp[prices.length-1][1]);
    }

}
